package com.tenglong.iservice.service.impl;

import com.tenglong.iservice.dao.ServiceMapper;
import com.tenglong.iservice.pojo.Service;
import com.tenglong.iservice.pojo.ServiceExample;
import com.tenglong.iservice.service.IserviceService;
import com.tenglong.util.BaseResult;
import com.tenglong.util.IDUtils;
import org.apache.commons.collections.BagUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Chen-D.W on 2018/5/24.
 */
@org.springframework.stereotype.Service("iserviceServiceImpl")
public class IserviceServiceImpl implements IserviceService {

    @Autowired
    private ServiceMapper serviceMapper;

    private static final double SOO=0.24;

    @Override
    public BaseResult getUserServiceById(String  id,Long flag) {
        ServiceExample example=new ServiceExample();
        ServiceExample.Criteria criteria=example.createCriteria();
        if (flag==1L){
            criteria.andUseIdEqualTo(id);
        }else {
            return BaseResult.ok(serviceMapper.selectByPrimaryKey(id));
        }
        List<Service> service=serviceMapper.selectByExample(example);
        if (service.size()!=0){
            return BaseResult.ok(service);
        }
        return BaseResult.build(400,"该客户没有购买任何服务");
    }

    @Override
    public BaseResult editService(Service service) {
        reckon(service);
        int result=serviceMapper.updateByPrimaryKeySelective(service);
        if (result!=0){
            String id=service.getUseId();
            return BaseResult.build(100,"成功了",id);
        }
        return BaseResult.build(400,"更新失败了");
    }

    @Override
    public List<Service> getService(String userid) {
        ServiceExample example=new ServiceExample();
       ServiceExample.Criteria criteria= example.createCriteria();
       criteria.andUseIdEqualTo(userid);
        List<Service> services=serviceMapper.selectByExample(example);
        return services;
    }

    @Override
    public BaseResult addService(Service service) {
        if(service.getUseId()==null||service.getUseId()==""){
            return BaseResult.build(400,"请先选添加或选择客户");
        }
        try {
            service.setId(String.valueOf(IDUtils.genItemId()));
            reckon(service);
        } catch (Exception e) {
            return BaseResult.build(400,e.getMessage());
        }
        int result=serviceMapper.insertSelective(service);
        if (result!=0){
            return BaseResult.ok(service);
        }
        return BaseResult.build(400,"添加失败了");
    }

    private Service reckon(Service service){

        try {
            double actual=Double.parseDouble(service.getIncomeActual());
            double cost=Double.parseDouble(service.getIncomeCost());
            double prof=actual-cost;
            double price=Double.parseDouble(service.getServicePrice());
            Long num=service.getServiceNum();
            double count=num*price;
            service.setIncomeProfits(String.valueOf(prof));
            double flag=prof/actual;
            if (flag>SOO){
                service.setIncomeWage(service.getIncomeActual());
            }else {
                service.setIncomeWage(String.valueOf(prof*4.16));
            }
            service.setServiceCount(String.valueOf(count));
            return service;
        } catch (NumberFormatException e) {
            throw new RuntimeException("请注意数据输入是否正确");
        }
    }

    @Override
    public BaseResult removeServiceById(String id) {
        int result=serviceMapper.deleteByPrimaryKey(id);
        if (result!=0){
            return BaseResult.ok();
        }
        return BaseResult.build(400,"未成功删除");
    }
}
